//
//  NcitBleDevice.h
//  BleHrateDemo
//
//  Created by Yongfeng Li on 8/15/14.
//  Copyright (c) 2014 Yongfeng Li. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>

#define HEARTRATENUMBER             @"HeartRateNumber"
#define HEARTRATENUMBERHISTORY      @"HeartRateNumberHistory"

@protocol NcitBleDevDelegate <NSObject>


@optional
//- (void) MyCoreBlueData:(id)data andMark:(NSString *)mark;
//- (void) isConnectState:(NSInteger)code;
- (void) didDiscoverPerServices:(CBPeripheral *)aPeripheral bleMgr:(CBCentralManager*)pBleMgr;
- (void) bleMgrDiscover:(CBPeripheral *)aPeripheral;

- (void) recvedBleWgtData:(double)dWgt fat:(float)fFat wat:(float)fWat mus:(float)fMus bone:(float)fBone wUnit:(short)nUn fatwatInc:(BOOL)bFatWat hgt:(float)fHgt;
- (void) recvedVdrBleWgtData:(double)dWgt fat:(float)fFat wat:(float)fWat mus:(float)fMus bone:(float)fBone wUnit:(short)nUn fatwatInc:(BOOL)bFatWat hgt:(float)fHgt wgt:(short)nWgt;

- (void) recvedBleDevSyncData:(const uint8_t *)rcvData len: (NSUInteger)nDataLen char:(CBCharacteristic *)pChar;
- (void) recvedBlePedoStepData:(const uint8_t *)rcvData len: (NSUInteger)nDataLen;
- (void) recvedBleOthData:(NSString*) data;
//- (void) recvedBleWgtData: (NSData *)data;

- (void) sendDataToBleDev:(CBCharacteristic*)aChar;
- (void) sendDataToYunmaiBleDev:(CBCharacteristic*)aChar;
//- (void) sendDataToPedoDev:(CBCharacteristic*)aChar;

- (void) recvedHrateData:(short)nHrate;
- (void) bleSupport:(BOOL)bSupport;

- (void) beginMeasBldPre;
- (void) updateBldPreVal:(short)nPresVal;
- (void) recvedBldPreVal:(Byte)nhPres lPres: (Byte)nlPres hRate:(Byte)nhRate;

- (void) updateBatteryLevel:(short)nLevel;

- (void) bleDevConnected;
- (void) bleDevConnected:(NSString*)sUuid;

- (void) bleDisconnected;
- (void) bleDevNoSyncFunc;

- (void) bleUserClosed;

- (void) bleDiscoverFailed;
- (void) onBleDevDiscoverTimeOut;
-(void)getDevice:(NSString *)deviceName;
@end

@interface NcitBleDevice : NSObject<CBCentralManagerDelegate, CBPeripheralDelegate>
@property(assign, nonatomic) id<NcitBleDevDelegate> delegate;
@property (assign, nonatomic) BOOL bAutoConnect;

@property (strong) id<NcitBleDevDelegate> bleDelegate;
@property (strong) CBPeripheral*       m_pBleDevPer;
@property (strong) CBCharacteristic*   pBleDevChar;
@property (strong) CBCharacteristic*   pBleSyncChar;

@property (assign, nonatomic) BOOL bLoadSavedBleDev;
@property (assign, nonatomic) BOOL bConnBleDev;
@property (assign, nonatomic) BOOL bJustScanDev;
@property (assign, nonatomic) BOOL bSyncDevData;

@property (assign, nonatomic) Byte nDevTypeIdx;
@property (retain, nonatomic) NSString* sStoredStepUuid;

//debug
@property (assign, nonatomic) NSUInteger nDebugDataType;

- (id) initBleDevice;

- (void) startMeas:(NSString*)sBleUUID;
- (void) startScan:(NSString*)sBleUUID;

- (void) stopMeas;
- (void) stopScan;

- (NSString*) getCurtStoredUuid:(short)nTypeIdx;
- (void) addSavedDevice:(NSString*)sUuid tryConn:(BOOL)bConn type:(short)nTypeIdx scan:(BOOL)bScan;
- (BOOL) loadSavedDevice:(short)nTypeIdx;

- (void) sendDataToBleDev:(CBCharacteristic*)aChar data:(NSData*) valData type:(CBCharacteristicWriteType)hdType;
- (BOOL) sendBodyInfoToYunmaiDev:(CBCharacteristic*)aChar data:(NSData*) valData type:(CBCharacteristicWriteType)hdType;

- (NSUInteger) connBLEDevCount;
- (NSUInteger) connBLEDevCount:(NSString*)sNamePre;
- (NSString*) getBlePerName;

- (BOOL) isHrmDevice:(NSString*) sBleName;
- (BOOL) isMemroyHrmDev;
- (BOOL) isMemroyHrm4:(NSString*) sBleName;

- (void) setConnBleName:(NSString*) sBleName;

- (CBPeripheral*) listBleDev:(NSInteger)nIdx;
- (void) retrievePeripherals:(NSArray *)peripheralUUIDs;
- (void) cancelPeripheralConnection/*:(CBPeripheral *)peripheral*/;

- (void) disablePedoSyncSvr;
- (void) enabHratNotification;
- (void) enableSyncChar:(CBCharacteristic*)pChar enable:(BOOL)bEnable;
- (void) enablePedoDevMeas;


- (void) resetCurtSelUuid;
- (BOOL) isStoredUuid:(short)nTypeIdx;

//debug
- (void) sendDebugHrate:(short)nHrate dtStart:dtMeas dtEnd:dtLast count:(int)nIdx;
//- (void) sendDebugHrate:(short)nHrate time:(NSDate*)dtMeas count:(int)nIdx;

- (BOOL) isTestHRMDevice;

@end
